//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.tax;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import org.jpgolay.gui.fixed.TableViewer;
import org.jpgolay.me.persistence.ObjectCRUD;
import org.jpgolay.me.util.MEUtility;
import org.jpgolay.me.util.Parameter;

// Referenced classes of package org.jpgolay.me.tax:
//            TaxGUI, Tax
public class TaxController extends TaxGUI {

    public TaxController(Parameter param) {
        write = false;
        this.param = param;
        crud = new ObjectCRUD();
        initLaw();
        initTaxTable();
        initTaxTableValues();
    }

    private void initLaw() {
        if (param.getAccessLaw().length() >= 9 && param.getAccessLaw().charAt(9) == '1') {
            write = true;
        }
    }

    private void initTaxTable() {
        getTaxViewer().setTypes(new Class[]{
                    java.lang.Integer.class, java.math.BigDecimal.class
                });
        getTaxViewer().setLastColumnNotEditable(0);
        getTaxViewer().setCellEditable(write);
        getTaxViewer().setAlternateColor(MEUtility.TAX_LINE);
        getTaxViewer().setColumnNames(new String[]{
                    "TVA", "Valeur (%)"
                });
        getTaxViewer().setPreferredColumnSize(new int[]{
                    50, 150
                });
    }

    private void initTaxTableValues() {
        java.util.Collection list = crud.readAll(null, 18);
        if (list != null) {
            Tax tax = null;
            ArrayList taxList = new ArrayList(list);
            taxData = new Object[taxList.size()][2];
            Iterator it = taxList.iterator();
            for (int i = 0; it.hasNext(); i++) {
                tax = (Tax) it.next();
                taxData[i][0] = tax.getId();
                taxData[i][1] = tax.getTaux().setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
            }

        } else {
            taxData = new Object[5][2];
            for (int i = 0; i < 5; i++) {
                taxData[i][0] = null;
                taxData[i][1] = null;
            }

        }
        getTaxViewer().setData(taxData);
        getTaxViewer().getTableModel().addTableModelListener(new TableModelListener() {
            public void tableChanged(TableModelEvent e) {
                actionUpdateRow(e.getFirstRow());
            }
        });
    }

    private void actionUpdateRow(int row) {
        Tax tax = (Tax) crud.read(getTaxViewer().getTable().getValueAt(row, 0), 18);
        if (tax != null) {
            tax.setTaux((BigDecimal) getTaxViewer().getTable().getValueAt(row, 1));
            crud.update(tax, 18);
        }
    }
    private Object taxData[][];
    private ObjectCRUD crud;
    private Parameter param;
    private boolean write;
}
